from pandac.PandaModules import TextNode
from panda3d.core import Vec2,Point3,Vec3,Vec4
from direct.interval.IntervalGlobal import *
import sys
'''
	Andrew Oberlin, Michael Jacobs, Tom Williams
	Date: February 21, 2012
'''
class GameStats:
	'''
		Creates the statistics needed to run the game
	'''
	def __init__(self, parent):
		self.parent = parent
		self.lives = 3
		self.coins = 0
		
		self.loadModels()
		
	'''
		Adds a coin to the number of coins. If the number exceeds 100,
		it wraps back to zero adding an extra life
	'''
	def addCoin(self):
		self.coins += 1
		if (self.coins == 100):
			self.coins = 0
			self.lives += 1
		self.parent.coinSound.play()
	
	
	def win(self):
		self.parent.level.destroyMap()
		self.parent.overworld.show()
		self.parent.camera.setPos(self.parent.WIN_WIDTH/2.0,self.parent.WIN_HEIGHT/2,50)
		self.parent.level.mario.model.hide()
	
	def died(self):
		self.lives -= 1;
		self.parent.level.destroyMap()
		
		self.parent.overworld.show()
		self.parent.camera.setPos(self.parent.WIN_WIDTH/2.0,self.parent.WIN_HEIGHT/2.0,50)
		self.parent.level.mario.model.hide()
		
		if(self.lives <= 0):
			self.parent.overworld.showGameOver()
			#sys.exit()
			
		
		
	
	'''
		Creates the coin icon and the number total, the extra life icon
		and the number total
	'''
	def loadModels(self):
		self.coinDummy = render.attachNewNode('number_coins_dummy')
		self.livesDummy = render.attachNewNode('number_lives_dummy')
		self.coinDummy.setPos(0, self.parent.WIN_HEIGHT*0.95, 0)
		self.livesDummy.setPos(0, self.parent.WIN_HEIGHT*0.95, 0)
		
		self.livesIcon = self.parent.loader.loadModelCopy("models/plane")
		self.livesTexture = self.parent.loader.loadTexture("models/levels/menu_items/lives_icon.png")
		self.livesIcon.setTexture(self.livesTexture)
		self.set2DProps(self.livesIcon, self.livesDummy)
		
		self.coinIcon = self.parent.loader.loadModelCopy("models/plane")
		self.coinTexture = self.parent.loader.loadTexture("models/levels/objects/coin.png")
		self.coinIcon.setTexture(self.coinTexture)
		self.set2DProps(self.coinIcon, self.coinDummy)
		
		# creates the text for displaying the number of coins currently
		self.numCoins = TextNode('num_coins')
		self.numCoins.setText("x00")
		self.numCoinsDummy = self.coinDummy.attachNewNode(self.numCoins)
		self.numCoinsDummy.setPos(20,-10,0)
		self.numCoinsDummy.setScale(30)
		self.numCoinsDummy.setHpr(Vec3(0,-90,0))
		# maximize efficiency
		self.numCoinsDummy.setBin("unsorted", 0)
		self.numCoinsDummy.setDepthTest(False)
		
		# creates the text for displaying the number of lives currently
		self.numLives = TextNode('num_lives')
		self.numLives.setText("x00")
		self.numLivesDummy = self.livesDummy.attachNewNode(self.numLives)
		self.numLivesDummy.setPos(20,-10,0)
		self.numLivesDummy.setScale(30)
		self.numLivesDummy.setHpr(Vec3(0,-90,0))
		# maximize efficiency
		self.numLivesDummy.setBin("unsorted", 0)
		self.numLivesDummy.setDepthTest(False)
		
		self.followCameraTask = self.parent.taskMgr.add(self.followCamera, 'stats_follow_camera')
		
	'''
		Sets the properties for a two dimensional figure
	'''
	def set2DProps(self, model, dummy, width=None):
		if (width == None):
			width = self.parent.BLOCK_SIZE
			
		model.setHpr(Vec3(0,-90,0))
		model.setScale(Vec3(width, self.parent.BLOCK_SIZE, self.parent.BLOCK_SIZE))
		
		# maximize efficiency
		model.setBin("unsorted", 0)
		model.setDepthTest(False)
		model.setTransparency(1)
		
		#self.model.setTexture(self.texture)
		model.reparentTo(dummy)
		
	'''
		Makes the coin heads up display follow the camera
	'''
	def followCamera(self, task):
		self.coinDummy.setX(self.parent.camera.getX() - self.parent.WIN_WIDTH*0.4)
		self.livesDummy.setX(self.parent.camera.getX() + self.parent.WIN_WIDTH*0.25)
		
		self.numLives.setText('x%02d' % self.lives)
		self.numCoins.setText('x%02d' % self.coins)
		
		return task.cont
		
		